package com.imooc.seataorder;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2021-02-12 09:00
 */

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderService {

//    private final RestTemplate restTemplate;

    private final ProductFeignClient productFeignClient;

    @GlobalTransactional
    public Boolean create(Integer count) {

        // 调用product微服务扣减库存
//        String url = "http://localhost:8086/deduct?productId=5001&count=" + count;
//        Boolean result = restTemplate.getForObject(url, Boolean.class);

        Boolean result = productFeignClient.deduct(5001L, count);

        if (result != null && result) {
            // 可能抛异常
            if (count == 5) {
                throw new RuntimeException("OrderService运行时异常");
            }
            log.info("数据库开始创建订单");
            return true;
        }
        return false;
    }
}
